using AStarOnGrid;
using Services;
using System.Collections.Generic;
using UnityEngine;

public class PathFindingSample : MonoBehaviour
{
    private Enemy enemy;
    private ActionController actionController;

    private Dictionary<Vector2Int, AStarNode> map;

    private void Start()
    {
        actionController = ServiceLocator.Get<ActionController>();
        enemy = GetComponent<Enemy>();
    }

    private void Update()
    {
        if (Input.GetMouseButtonDown(4))
            Move();
        if (Input.GetMouseButtonDown(3))
            Display();
    }

    private void Display()
    {
        Vector2Int target = ActionController.GlobalGrid.WorldToCell(InputHelper.MouseWorldPosition2D());
        map = NPCAI.FindPath(enemy, target, false, null, true);
    }

    private void Move()
    {
        Vector2Int target = ActionController.GlobalGrid.WorldToCell(InputHelper.MouseWorldPosition2D());
        enemy.GridTransform.Position = NPCAI.NextStep(enemy, target);
    }

    private void OnDrawGizmos()
    {
        if (map != null)
        {
            foreach (AStarNode node in map.Values)
            {
                Gizmos.color = node.Type switch
                {
                    ENodeType.Blank => Color.white,
                    ENodeType.Open => Color.green,
                    ENodeType.Close => Color.red,
                    ENodeType.Route => Color.blue,
                    _ => Color.magenta,
                };
                Gizmos.color = Gizmos.color.SetAlpha(0.5f);
                Gizmos.DrawCube(ActionController.GlobalGrid.CellToWorld(node.Position), Vector3.one);
            }
        }
    }

}
